import { _decorator, Component, instantiate, Node, Prefab, v3, Vec3 } from 'cc';
import { Tile } from './Tile';
import ChessboardState from './ChessboardState';
const { ccclass, property, executeInEditMode } = _decorator;

@ccclass('GridController')
@executeInEditMode(true)
export class GridController extends Component {

    @property({
        type: Prefab
    })
    tile: Prefab = null;

    /**左上角圆角 */
    @property(Prefab) public topLeft: Prefab = null!;

    /**右上角圆角 */
    @property(Prefab) public topRight: Prefab = null!;

    /**左下圆角 */
    @property(Prefab) public bottomLeft: Prefab = null!;

    /**右下圆角 */
    @property(Prefab) public bottomRight: Prefab = null!;


    /**顶部围墙 */
    @property(Prefab) public topSide: Prefab = null!;

    /**底部围墙 */
    @property(Prefab) public bottomSide: Prefab = null!;

    /**左边围墙 */
    @property(Prefab) public leftSide: Prefab = null!;

    /**右边围墙 */
    @property(Prefab) public rightSide: Prefab = null!;




    @property({
        type: Vec3
    })
    sizeGrid: Vec3 = v3(5, 1, 5)


    cellSize: Vec3 = v3(2, 1, 2)


    @property({

    })
    get createGaudBtn() { return false };
    set createGaudBtn(v: boolean) {
        if (v) {
            this.createGridInEditor();
        }
    };

    start() {

    }

    getTileList() {
        return this.node.children.filter(item => !!item.getComponent(Tile)).map(item => item.getComponent(Tile))
    }

    getTopRightPos() {
        let tile = this.node.children[0];
        let tileWordPos = tile.getWorldPosition().clone();
        return new Vec3(tileWordPos.x - 1, 0, tileWordPos.z + 1)
    }

    createGridInEditor() {
        //计算出起始位置
        const startX = -((this.sizeGrid.x / 2) * this.cellSize.x);
        const startZ = ((this.sizeGrid.z / 2) * this.cellSize.z);

        for (let i = 0; i < this.sizeGrid.x; i++) {
            for (let j = 0; j < this.sizeGrid.z; j++) {
                let newTile = instantiate(this.tile);
                newTile.getComponent(Tile).indexXZ = [i, j];
                newTile.setParent(this.node);
                newTile.setPosition(startX + i * this.cellSize.x, 0, startZ - j * this.cellSize.z);
            }
        }

        for (let i = 0; i < this.sizeGrid.x; i++) {
            let topNode = instantiate(this.topSide);
            topNode.setParent(this.node);
            topNode.setPosition(startX + 1 + i * 2, 0, startZ + 1 * 2)


            let bottomNode = instantiate(this.bottomSide);
            bottomNode.setParent(this.node);
            bottomNode.setPosition(startX - 1 + i * 2, 0, startZ - (this.sizeGrid.z * 2))
        }

        for (let i = 0; i < this.sizeGrid.z; i++) {
            let leftNode = instantiate(this.leftSide);
            leftNode.setParent(this.node);
            leftNode.setPosition(startX - 2, 0, startZ + 1 - i * 2)


            let righNode = instantiate(this.rightSide);
            righNode.setParent(this.node);
            righNode.setPosition(startX + this.sizeGrid.x * 2, 0, startZ - 1 - (i * 2))
        }

        let topLeft = instantiate(this.topLeft);
        topLeft.setParent(this.node);
        topLeft.setPosition(startX - 1, 0, startZ + 1)

        let bottomLeft = instantiate(this.bottomLeft);
        bottomLeft.setParent(this.node);
        bottomLeft.setPosition(startX - 1, 0, startZ - (this.sizeGrid.z * 2) + 1);

        let topRight = instantiate(this.topRight);
        topRight.setParent(this.node);
        topRight.setPosition(startX + (this.sizeGrid.z * 2) - 1, 0, startZ + 1)


        let bottomRight = instantiate(this.bottomRight);
        bottomRight.setParent(this.node);
        bottomRight.setPosition(startX + (this.sizeGrid.z * 2) - 1, 0, startZ - (this.sizeGrid.z * 2) + 1)
    }

    update(deltaTime: number) {

    }
}


